home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH5 / EMAGA5 BOOK CODE / control / server / weapons / weapon.cs < prev   
Text File  |  2006-09-18  |  6KB  |  171 lines

  1. //============================================================================
  2. // control/players/weapon.cs
  3. //
  4. // Copyright (c) 2003,2006  Kenneth C. Finney
  5. // Portions Copyright (c) 2001 GarageGames.Com
  6. // Portions Copyright (c) 2001 by Sierra Online, Inc.
  7. //============================================================================
  8.  
  9. // This file contains Weapon and Ammo Class/"namespace" helper methods
  10. // as well as hooks into the inventory system. These functions are not
  11. // attached to a specific C++ class or datablock, but define a set of
  12. // methods which are part of dynamic namespaces "class". The Items
  13. // include these namespaces into their scope using the  ItemData and
  14. // ItemImageData "className" variable.
  15.  
  16. // All ShapeBase images are mounted into one of 8 slots on a shape.
  17. // This weapon system assumes all primary weapons are mounted into
  18. // this specified slot:
  19. $WeaponSlot = 0;
  20. //-----------------------------------------------------------------------------
  21. // Audio profiles
  22.  
  23. datablock AudioProfile(WeaponUseSound)
  24. {
  25.    filename = "~/data/sound/weapon_switch.wav";
  26.    description = AudioClose3d;
  27.     preload = true;
  28. };
  29.  
  30. datablock AudioProfile(WeaponPickupSound)
  31. {
  32.    filename = "~/data/sound/weapon_pickup.wav";
  33.    description = AudioClose3d;
  34.     preload = true;
  35. };
  36.  
  37. datablock AudioProfile(AmmoPickupSound)
  38. {
  39.    filename = "~/data/sound/ammo_pickup.wav";
  40.    description = AudioClose3d;
  41.     preload = true;
  42. };
  43. //-----------------------------------------------------------------------------
  44. // Weapon Class
  45. //-----------------------------------------------------------------------------
  46.  
  47. function Weapon::onUse(%data,%obj)
  48. {
  49.    // Default behavoir for all weapons is to mount it into the
  50.    // this object's weapon slot, which is currently assumed
  51.    // to be slot 0
  52.    if (%obj.getMountedImage($WeaponSlot) != %data.image.getId())
  53.    {
  54.       %obj.mountImage(%data.image, $WeaponSlot);
  55.       if (%obj.client)
  56.          messageClient(%obj.client, 'MsgWeaponUsed', '\c0Weapon selected');
  57.    }
  58. }
  59.  
  60. function Weapon::onPickup(%this, %obj, %shape, %amount)
  61. {
  62.    // The parent Item method performs the actual pickup.
  63.    // For player's we automatically use the weapon if the
  64.    // player does not already have one in hand.
  65.    if (Parent::onPickup(%this, %obj, %shape, %amount))
  66.    {
  67.       if ( (%shape.getClassName() $= "Player" ||
  68.             %shape.getClassName() $= "AIPlayer"  )  &&
  69.             %shape.getMountedImage($WeaponSlot) == 0)
  70.       {
  71.          %shape.use(%this);
  72.       }
  73.    }
  74. }
  75.  
  76. function Weapon::onInventory(%this,%obj,%amount)
  77. {
  78.    // Weapon inventory has changed, make sure there are no weapons
  79.    // of this type mounted if there are none left in inventory.
  80.    if (!%amount && (%slot = %obj.getMountSlot(%this.image)) != -1)
  81.       %obj.unmountImage(%slot);
  82. }
  83.  
  84.  
  85. //-----------------------------------------------------------------------------
  86. // Weapon Image Class
  87. //-----------------------------------------------------------------------------
  88.  
  89. function WeaponImage::onMount(%this,%obj,%slot)
  90. {
  91.    // Images assume a false ammo state on load.  We need to
  92.    // set the state according to the current inventory.
  93.    if (%obj.getInventory(%this.ammo))
  94.       %obj.setImageAmmo(%slot,true);
  95. }
  96.  
  97.  
  98. //-----------------------------------------------------------------------------
  99. // Ammmo Class
  100. //-----------------------------------------------------------------------------
  101.  
  102. function Ammo::onPickup(%this, %obj, %shape, %amount)
  103. {
  104.    // The parent Item method performs the actual pickup.
  105.    if (Parent::onPickup(%this, %obj, %shape, %amount))
  106.    {
  107.  
  108.    }
  109. }
  110.  
  111. function Ammo::onInventory(%this,%obj,%amount)
  112. {
  113.    // The ammo inventory state has changed, we need to update any
  114.    // mounted images using this ammo to reflect the new state.
  115.    for (%i = 0; %i < 8; %i++)
  116.    {
  117.       if ((%image = %obj.getMountedImage(%i)) > 0)
  118.          if (IsObject(%image.ammo) && %image.ammo.getId() == %this.getId())
  119.             %obj.setImageAmmo(%i,%amount != 0);
  120.    }
  121. }
  122.  
  123. // Support function which applies damage to objects within the radius of
  124. // some effect, usually an explosion.  This function will also optionally
  125. // apply an impulse to each object.
  126.  
  127. function radiusDamage(%sourceObject, %position, %radius, %damage, %damageType, %impulse)
  128. {
  129.    // Use the container system to iterate through all the objects
  130.    // within our explosion radius.  We'll apply damage to all ShapeBase
  131.    // objects.
  132.    InitContainerRadiusSearch(%position, %radius, $TypeMasks::ShapeBaseObjectType);
  133.  
  134.    %halfRadius = %radius / 2;
  135.    while ((%targetObject = containerSearchNext()) != 0) {
  136.  
  137.       // Calculate how much exposure the current object has to
  138.       // the explosive force.  The object types listed are objects
  139.       // that will block an explosion.  If the object is totally blocked,
  140.       // then no damage is applied.
  141.       %coverage = calcExplosionCoverage(%position, %targetObject,
  142.          $TypeMasks::InteriorObjectType |  $TypeMasks::TerrainObjectType |
  143.          $TypeMasks::ForceFieldObjectType | $TypeMasks::VehicleObjectType);
  144.       if (%coverage == 0)
  145.          continue;
  146.  
  147.       // Radius distance subtracts out the length of smallest bounding
  148.       // box axis to return an appriximate distance to the edge of the
  149.       // object's bounds, as opposed to the distance to it's center.
  150.       %dist = containerSearchCurrRadiusDist();
  151.  
  152.       // Calculate a distance scale for the damage and the impulse.
  153.       // Full damage is applied to anything less than half the radius away,
  154.       // linear scale from there.
  155.       %distScale = (%dist < %halfRadius)? 1.0:
  156.          1.0 - ((%dist - %halfRadius) / %halfRadius);
  157.  
  158.       // Apply the damage
  159.       %targetObject.damage(%sourceObject, %position,
  160.          %damage * %coverage * %distScale, %damageType);
  161.  
  162.       // Apply the impulse
  163.       if (%impulse) {
  164.          %impulseVec = VectorSub(%targetObject.getWorldBoxCenter(), %position);
  165.          %impulseVec = VectorNormalize(%impulseVec);
  166.          %impulseVec = VectorScale(%impulseVec, %impulse * %distScale);
  167.          %targetObject.applyImpulse(%position, %impulseVec);
  168.       }
  169.    }
  170. }
  171.